package com.ingenio.siot;


import java.sql.*;
import java.util.*;

public class ConexionPool implements IConexionPool{

	protected Stack<Connection> pool;
	protected String connectionURL;
    protected String userName;
    protected String password;
    
    public ConexionPool(String aConnectionURL, String aUserName, String aPassword)
    {
	 connectionURL = aConnectionURL;
	 userName = aUserName;
	 password = aPassword;
	 pool = new Stack<Connection>();
    }
    
	@Override
	public Connection extraerConexion() throws SQLException {
		
		// Si el pool no esta vacio, tomar una conexion  
		if(!pool.empty()) 
		    { 
			 return (Connection) pool.pop();
		    } 
		else 
		    { 
			  // Entonces generar una conexion nueva
			  return DriverManager.getConnection(connectionURL, userName, password);
		    } 
	}

	@Override
	public void liberarConexion(Connection conn) {
		pool.push(conn);
	}

}
